package com.feihong.txgw.utils;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.SPUtils;
import com.feihong.txgw.model.bean.searchList.HisToryEntity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

/**
 * @author wangziheng
 * @name txgw_app
 * @class name：com.feihong.txgw.utils
 * @class describe:
 * @time 2017/11/24 15:54
 * @change
 * @chang time
 * @class describe
 */
public class HistoryUtil {

    private List<HisToryEntity> mHistoryList;
    private LinkedHashMap<String, Long> mHistoryMap;
    private SPUtils mSpUtils;
    private static final HistoryUtil ourInstance = new HistoryUtil();

    public static HistoryUtil getInstance() {
        return ourInstance;
    }

    private HistoryUtil() {

    }

    /**
     * 获取历史记录
     *
     * @return
     */
    public List<HisToryEntity> queryHistoy() {
        if (mHistoryList != null) {
            mHistoryList.clear();
        }
        Set<String> set = mHistoryMap.keySet();
        for (String key : set) {
            HisToryEntity hisToryEntity = new HisToryEntity();
            hisToryEntity.setHistoryContent(key);
            hisToryEntity.setTime(mHistoryMap.get(key));
            mHistoryList.add(hisToryEntity);
        }
        return mHistoryList;
    }

    /**
     * 从本地获取历史记录
     * @return
     */
    public List<HisToryEntity> getHistoryFromLocal() {
        mSpUtils = SPUtils.getInstance("localSelect");
        String mHistotyStr = mSpUtils.getString("name");
        if (mHistoryMap == null) {
            mHistoryMap = new LinkedHashMap<>();
        }else {
            mHistoryMap.clear();
        }
        if (mHistoryList == null) {
            mHistoryList = new ArrayList<>();
        }else {
            mHistoryList.clear();
        }
        if (!mHistotyStr.isEmpty()) {
            try {
                JSONArray jsonArray = new JSONArray(mHistotyStr);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject histotyJsonObj = jsonArray.optJSONObject(i);
                    HisToryEntity hisToryEntity = JSON.parseObject(histotyJsonObj.toString(), HisToryEntity.class);
                    mHistoryMap.put(hisToryEntity.getHistoryContent(), hisToryEntity.getTime());
                    mHistoryList.add(hisToryEntity);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return mHistoryList;
    }

    /**
     * 暂时保存历史记录
     *
     * @param key
     */
    public void setHistory(String key) {
        if (mHistoryMap == null) {
            mHistoryMap = new LinkedHashMap<>();
        }
        if(mHistoryMap.containsKey(key)){
            mHistoryMap.remove(key);
        }
        mHistoryMap.put(key, System.currentTimeMillis());
    }

    /**
     * 删除单项历史记录
     *
     * @param key
     * @return
     */
    public int remove(String key) {
        mHistoryMap.remove(key);
        return mHistoryMap.size();
    }

    /**
     * 删除所有所有历史记录
     */
    public void removeAll() {
        mHistoryMap.clear();
        mHistoryList.clear();
    }

    /**
     * 将历史记录保存到本地
     */
    public void savaHistoryToLocal() {
        if (mHistoryMap == null) {
            mHistoryMap = new LinkedHashMap<>();
        }
        Set<String> keySet = mHistoryMap.keySet();
        List<HisToryEntity> histotyList = new ArrayList<>();
        for (String key : keySet) {
            HisToryEntity hisToryEntity = new HisToryEntity();
            hisToryEntity.setHistoryContent(key);
            hisToryEntity.setTime(hisToryEntity.getTime());
            histotyList.add(hisToryEntity);
        }
        mSpUtils.put("name", JSON.toJSONString(histotyList));
    }

    public void onDestory(){
        if(mHistoryMap != null){
            mHistoryMap.clear();
            mHistoryMap = null;
        }
        if(mHistoryList != null){
            mHistoryList.clear();
            mHistoryList = null;
        }
    }
}
